//JqGrid Template
var numberTemplate = {formatter: 'number', align: 'right', sorttype: 'number'};	 
var moneyTemplate = {formatter: 'number', align: 'right', sorttype: 'number',formatoptions:{decimalPlaces:0}};
var dateTemplate = {formatter:'date',formatoptions:{srcformat: 'Y-m-d',newformat: 'd-M-Y',}};
		
// add module text search to jqgrid
$(function(){
	//add jqgrid module search
	if (typeof($.jgrid.extend) != "undefined") { 
		$.jgrid.extend({
			addSearchText :function(searchField){
				var $t = this[0];
				$("#gview_" + $t.id).prepend('<div id="src'+ $t.id + '"  style="padding:2px"><input id="idSrc'+ $t.id + '" type="search" size="50" value="" class="ui-pg-input" style="font-size:13px;height:19px;bolder:1px solid"></div>');			
				$("#idSrc" + $t.id).on("keyup search",function(){
					var f;
					var grid = $("#" + $t.id);
					var searchFilter = $("#idSrc" + $t.id).val();
					if (searchFilter.length === 0) {
						grid[0].p.search = false;
						$.extend(grid[0].p.postData,{filters:""});
					}
					f = {groupOp:"OR",rules:[]};
					if ($.isArray(searchField)){
						for(var i = 0;i < searchField.length;i++){
							f.rules.push({field:searchField[i],op:"cn",data:searchFilter});
						}
					}else{
						f.rules.push({field:searchField,op:"cn",data:searchFilter});
					}
					grid[0].p.search = true;
					$.extend(grid[0].p.postData,{filters:JSON.stringify(f)});
					grid.trigger("reloadGrid",[{page:1,current:true}]);
				});			
				return this;
			}
		});
	}
	
	//format number only
	if (typeof($('.currency').maskMoney) != "undefined") { 
	    // safe to use the function
		$('.currency').maskMoney(
				{thousands:',', decimal:'.', allowZero:true, suffix: '',precision:0}
			);
	}
	
	//accounting.min.js
	if (typeof(accounting) != "undefined") { 
		accounting.settings = {
			currency: {
				symbol : "",   // default currency symbol is '$'
				format: "%s%v", // controls output: %s = symbol, %v = value/number (can be object: see below)
				decimal : ".",  // decimal point separator
				thousand: ",",  // thousands separator
				precision :0   // decimal places
			},
			number: {
				precision : 0,  // default precision on numbers is 0
				thousand: ",",
				decimal : "."
			}
		};
	}
});

function showLoader(){
	$("#loader").addClass("block");
}

function hideLoader(){
	$("#loader").removeClass("block");
}

function refreshDisplayCurrency(){
	$('.currency').maskMoney("mask");
	$('label.currency').each(function(){
		$(this).html(formatMoney($(this).html()));
	});
	
}

function openWindow(winName,url,width,height,onCenter,specs,replace){
	if (onCenter){
		var left = parseInt((screen.availWidth/2) - (width/2));
		var top = parseInt((screen.availHeight/2) - (height/2));
		specs = specs + ",width=" + width + ",height=" + height + ",left=" + left + ",top=" + top;
	}else{
		specs = specs + ",width=" + width + ",height=" + height + ",left=0,top=0";
	}
	return window.open(url,winName,specs,replace);
}


function renewSelectData(data,objTarget,boolAll,controler){	
	//var filter = {"is_active":1,"department_id":$("#department_id").val()};
	//var data = {obj:"MTahunAjaran",filter: filter,value_field:"id",title_field:"name"};
	controler = typeof controler !== 'undefined' ? controler : "data_select/selectDataAjxResult";	
	var url = "index.php?r=" + controler;
	$.ajax({	
		url : url,
		type:"GET",
		async:false,
		data: data,
		success: function(respData) {
			printConsole(respData);
			fillSelectData(objTarget,respData,boolAll);	
		},
	});
}

function fillSelectData(objId,data,boolAll){
	$(objId).empty();
	boolAll = typeof boolAll !== 'undefined' ? boolAll : false;
	if (boolAll){
		$(objId).append("<option value=''>[ALL]</option>");
	}
	
	for (i=0;i<data.length;i++){
		$(objId).append("<option value='" + data[i].value + "'>" + data[i].title +"</option>");
	}		
}

function printConsole(obj){	
	console.log(obj);	
}

function getIntervalPeriod(startPeriod,endPeriod){
	try{
		if (startPeriod.length == 7 && endPeriod.length == 7){
			var endTh=  Number(endPeriod.substr(0,4));
			var endBln= Number(endPeriod.substr(5,2));
			var startTh=  Number(startPeriod.substr(0,4));
			var startBln= Number(startPeriod.substr(5,2));		
			var selisihTh = endTh - startTh;
			if (selisihTh >=0){
				endBln = endBln + (selisihTh * 12);
				if (endBln >= startBln){
					return (endBln - startBln) + 1;
				}else{
					return 0;
				}
			}else{
				return 0;
			}
			
		}else{
			return 0;
		}
	}catch(err){
		return 0;
	}	
}

function formatMoney(value){
	return accounting.formatMoney(value);
}

function setColumnSelect(objectId,separator,spacesToAdd){
	var biggestCol1 = 0;
	$("#" + objectId + " option").each(function(){
		var parts = $(this).text().split(separator);
		var len = parts[0].length;
	    if(len > biggestCol1){
	    	biggestCol1 = len;
	    }
	});
	
	var padLength = biggestCol1 + spacesToAdd;	
	$("#" + objectId + " option").each(function(){
	    var parts = $(this).text().split(separator);
	    var strLength = parts[0].length;
	    for(var x= strLength; x < (padLength); x++){
	        parts[0] = parts[0] + ' '; 
	    }
	    $(this).text(parts[0].replace(/ /g, '\u00a0')+ ' - ' + parts[1]).text;
	});
	
	$("#"+objectId).css("font-family","Courier New,Courier,monospace");
	
}
